package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
	"time"
)

// 获取体温通知信息
func GetTempNoticeInfo(p map[string]interface{}) *models.TempNotice {
	o := orm.NewOrm()
	tempNotice := &models.TempNotice{}
	qs := o.QueryTable("TempNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["deviceId"]; ok {
		qs = qs.Filter("device_id", p["deviceId"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	if _, ok := p["createdDate"]; ok {
		qs = qs.Filter("created_date", p["createdDate"])
	}
	if _, ok := p["isArrived"]; ok {
		qs = qs.Filter("is_arrived", p["isArrived"])
	}
	if _, ok := p["isLeft"]; ok {
		qs = qs.Filter("is_left", p["isLeft"])
	}
	qs.One(tempNotice)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if tempNotice.Id > 0 {
			tempNotice.ArrivedShow = tempNotice.Arrived.Format("2006-01-02 15:04:05")
			tempNotice.LeftShow = tempNotice.Left.Format("2006-01-02 15:04:05")
		}
	}
	return tempNotice
}

// 添加体温通知
func AddTempNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	tempNotice := &models.TempNotice{}
	device := &models.Device{}
	if _, ok := input["device_id"]; ok {
		device.Id = utils.Atoi64(input["device_id"][0])
		o.Read(device)
	}
	tempNotice.Device = device
	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	tempNotice.Student = student
	if _, ok := input["is_arrived"]; ok {
		tempNotice.IsArrived, _ = strconv.Atoi(input["is_arrived"][0])
	}
	if _, ok := input["is_left"]; ok {
		tempNotice.IsLeft, _ = strconv.Atoi(input["is_left"][0])
	}
	if _, ok := input["arrived"]; ok {
		loc, _ := time.LoadLocation("Local")
		theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", input["arrived"][0], loc)
		tempNotice.Arrived = theTime
	}
	if _, ok := input["left"]; ok {
		loc, _ := time.LoadLocation("Local")
		theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", input["left"][0], loc)
		tempNotice.Left = theTime
	}
	if _, ok := input["arrived_temp"]; ok {
		tempNotice.ArrivedTemp = utils.Atof64(input["arrived_temp"][0])
	}
	if _, ok := input["left_temp"]; ok {
		tempNotice.LeftTemp = utils.Atof64(input["left_temp"][0])
	}
	return o.Insert(tempNotice)
}

// 修改体温通知信息
func EditTempNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("TempNotice").Filter("id", input["id"][0]).Update(p)
}

// 批量修改体温通知信息
func EditTempNotices(p map[string]interface{}, v map[string]interface{}) (int64, error) {
	o := orm.NewOrm()
	qs := o.QueryTable("TempNotice")
	if _, ok := p["deviceId"]; ok {
		qs = qs.Filter("device_id", p["deviceId"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	if _, ok := p["createdDate"]; ok {
		qs = qs.Filter("created_date", p["createdDate"])
	}
	if _, ok := p["isArrived"]; ok {
		qs = qs.Filter("is_arrived", p["isArrived"])
	}
	if _, ok := p["isLeft"]; ok {
		qs = qs.Filter("is_left", p["isLeft"])
	}
	return qs.Update(v)
}

// 获取体温通知列表
func GetTempNoticeList(p map[string]interface{}) (int64, []*models.TempNotice) {
	var tempNoticeList []*models.TempNotice
	o := orm.NewOrm()
	qs := o.QueryTable("TempNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["deviceId"]; ok {
		qs = qs.Filter("device_id", p["deviceId"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	if _, ok := p["studentIds"]; ok && len(p["studentIds"].([]int64)) > 0 {
		qs = qs.Filter("student_id__in", p["studentIds"])
	}
	if _, ok := p["familySn"]; ok {
		qs = qs.Filter("Student__FamilySn", p["familySn"])
	}
	if _, ok := p["studentName"]; ok {
		qs = qs.Filter("Student__RealName__contains", p["studentName"])
	}
	if _, ok := p["createdDate"]; ok {
		qs = qs.Filter("created_date", p["createdDate"])
	}
	if _, ok := p["isArrived"]; ok {
		qs = qs.Filter("is_arrived", p["isArrived"])
	}
	if _, ok := p["isLeft"]; ok {
		qs = qs.Filter("is_left", p["isLeft"])
	}
	if _, ok := p["arrivedTemp"]; ok {
		qs = qs.Filter("arrived_temp__gt", p["arrivedTemp"])
	}
	if _, ok := p["leftTemp"]; ok {
		qs = qs.Filter("left_temp__gt", p["leftTemp"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&tempNoticeList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, tempNotice := range tempNoticeList {
			tempNotice.ArrivedShow = tempNotice.Arrived.Format("2006-01-02 15:04:05")
			tempNotice.ArrivedDateShow = tempNotice.Arrived.Format("2006-1-2")
			tempNotice.LeftShow = tempNotice.Left.Format("2006-01-02 15:04:05")
			tempNotice.LeftDateShow = tempNotice.Left.Format("2006-1-2")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, tempNotice := range tempNoticeList {
			tempNotice.StudentName = tempNotice.Student.RealName
			tempNotice.ArrivedDateShow = tempNotice.Arrived.Format("2006-1-2")
			tempNotice.LeftDateShow = tempNotice.Left.Format("2006-1-2")
		}
	}
	return totalCount, tempNoticeList
}
